package com.hello.api;

import com.Constants;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws IOException {
        simpleSpringRabbitmqUsage();
        //rpc风格的使用方式 https://www.jianshu.com/p/24b88ccfb019
    }


    private static void simpleSpringRabbitmqUsage() {
        // 用来创建Connection,此接口来自于spring-rabbit
        ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        // AmqpAdmin主要用来管理队列,交换机,绑定
        AmqpAdmin admin = new RabbitAdmin(connectionFactory);
        // 声明(创建)一个队列
        admin.declareQueue(new Queue(Constants.QUEUE_NAME));

        // AmqpTemplate主要用来发送以及接收消息
        AmqpTemplate template = new RabbitTemplate(connectionFactory);
        template.convertAndSend(Constants.QUEUE_NAME, "Hello World");

        String msg = (String) template.receiveAndConvert(Constants.QUEUE_NAME);
        System.out.println(msg);
    }


}
